package net.funwoo.pandago.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import net.funwoo.pandago.R;
import net.funwoo.pandago.ui.user.LoginActivity;
import net.funwoo.pandago.util.StringUtils;
import net.funwoo.pandago.util.UiUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Bob Peng on 2015/5/8.
 */
public class PersonMenuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_HEADER = 2;
    private static final int TYPE_ITEM = 1;
    private static final int TYPE_SEPARATOR = 0;

    private List<MenuInfo> mList;
    private HeaderInfo mHeaderInfo;
    private Context mContext;
    private int mCount;
    private DecimalFormat mDecimalFormat;

    public PersonMenuAdapter(Context context) {
        mContext = context;
        String[] menuArray = context.getResources().getStringArray(R.array.person_menu_list);
        mCount = menuArray.length + 3;
        mList = new ArrayList<>(mCount);

        MenuInfo menuInfo;

        // header separator
        addSeparator(0);

        // add header
        menuInfo = new MenuInfo(1, false, null, null, null, null);
        menuInfo.isSeparator = false;
        mList.add(menuInfo);

        // header separator
        addSeparator(2);

        // add menu
        int position = 3;
        for (String menu : menuArray) {
            menuInfo = new MenuInfo(position, true, menu, null, null, null);
            menuInfo.isSeparator = StringUtils.isEmpty(menuInfo.title);
            if (menuInfo.position > 10) {
                menuInfo.showIcon = false;
            }
            mList.add(menuInfo);

            position++;
        }

        mDecimalFormat = new DecimalFormat("￥00.0#");
    }

    private void addSeparator(int position) {
        MenuInfo menuInfo = new MenuInfo(position, false, null, null, null, null);
        menuInfo.isSeparator = true;
        mList.add(menuInfo);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == TYPE_HEADER) {
            view = View.inflate(mContext, R.layout.list_header_person, null);
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(-1, -2);
            view.setLayoutParams(params);
            return new HeaderViewHolder(view);
        } else if (viewType == TYPE_ITEM) {
            view = View.inflate(mContext, R.layout.list_item_person, null);
            return new MenuViewHolder(view);
        } else {
            view = new FrameLayout(mContext);
            return new SeparatorHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MenuViewHolder) {
            onBindMenuHolder((MenuViewHolder) holder, position);
        } else if (holder instanceof HeaderViewHolder) {
            onBindHeaderHolder((HeaderViewHolder) holder);
        } else if (holder instanceof SeparatorHolder) {
            if (position == 2) {
                ((SeparatorHolder) holder).view.getLayoutParams().height = UiUtils.dp2px(16);
            }
        }
    }

    private void onBindHeaderHolder(HeaderViewHolder holder) {
        if (mHeaderInfo != null) {
            Picasso.with(mContext)
                    .load(mHeaderInfo.avatarUrl)
                    .transform(UiUtils.getRoundedTransformation())
                    .into(holder.avatarView);
            holder.loginNameText.setText(mHeaderInfo.username);
        }
    }

    private void onBindMenuHolder(MenuViewHolder menuHolder, int position) {
        MenuInfo menuInfo = mList.get(position);
        if (!menuInfo.isSeparator) {
            menuHolder.iconView.setVisibility(menuInfo.showIcon ? View.VISIBLE : View.GONE);
            if (menuInfo.showIcon) {
                menuHolder.iconView.setImageDrawable(getMenuIcon(position));
            }

            menuHolder.titleText.setText(menuInfo.title);

            boolean showMoney = !TextUtils.isEmpty(menuInfo.money);
            menuHolder.moneyText.setVisibility(showMoney ? View.VISIBLE : View.GONE);
            if (showMoney) {
                menuHolder.moneyText.setText(mDecimalFormat.format(Double.parseDouble(menuInfo.money)));
            }

            boolean showContent = !TextUtils.isEmpty(menuInfo.content);
            menuHolder.contentText.setVisibility(showContent ? View.VISIBLE : View.GONE);
            if (showContent) {
                menuHolder.contentText.setText(menuInfo.content);
            }

            boolean showDot = !TextUtils.isEmpty(menuInfo.dotNumber);
            menuHolder.dotView.setVisibility(showDot ? View.VISIBLE : View.GONE);
            if (showDot) {
                menuHolder.dotView.setText(menuInfo.dotNumber);
            }
        }
    }

    private Drawable getMenuIcon(int position) {
        switch (position) {
            case 3:
                return mContext.getResources().getDrawable(R.drawable.ic_personal);
            case 4:
                return mContext.getResources().getDrawable(R.drawable.ic_wallet);
            case 5:
                return mContext.getResources().getDrawable(R.drawable.ic_order);
            case 6:
                return mContext.getResources().getDrawable(R.drawable.ic_comment);
            case 8:
                return mContext.getResources().getDrawable(R.drawable.ic_errands);
            case 9:
                return mContext.getResources().getDrawable(R.drawable.ic_auth);
        }
        return null;
    }

    public List<MenuInfo> getMenuList() {
        return mList;
    }

    public void setHeaderInfo(String url, String name) {
        mHeaderInfo = new HeaderInfo();
        mHeaderInfo.avatarUrl = url;
        mHeaderInfo.username = name;
    }

    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return position == 1 ? TYPE_HEADER : (mList.get(position).isSeparator ? TYPE_SEPARATOR : TYPE_ITEM);
    }

    public class HeaderViewHolder extends RecyclerView.ViewHolder {
        public LinearLayout headerItem;
        public ImageView avatarView;
        public TextView loginNameText;

        public HeaderViewHolder(View view) {
            super(view);
            headerItem = (LinearLayout) view.findViewById(R.id.person_account_info);
            headerItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO check login state
                    mContext.startActivity(new Intent(mContext, LoginActivity.class));
                }
            });
            avatarView = (ImageView) view.findViewById(R.id.person_avatar);
            loginNameText = (TextView) view.findViewById(R.id.person_login_name);
        }
    }

    public class MenuViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public RelativeLayout menuItem;
        public ImageView iconView;
        public TextView titleText;
        public TextView moneyText;
        public TextView contentText;
        public TextView dotView;

        public MenuViewHolder(View view) {
            super(view);
            menuItem = (RelativeLayout) view.findViewById(R.id.person_menu);
            iconView = (ImageView) view.findViewById(R.id.person_menu_icon);
            titleText = (TextView) view.findViewById(R.id.person_menu_title);
            moneyText = (TextView) view.findViewById(R.id.person_menu_money);
            contentText = (TextView) view.findViewById(R.id.person_menu_content);
            dotView = (TextView) view.findViewById(R.id.person_menu_dot);

            menuItem.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            int position = getAdapterPosition();
            // TODO menu click
            switch (position) {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 5:
                    break;
                case 6:
                    break;
            }
        }
    }

    public class SeparatorHolder extends RecyclerView.ViewHolder {

        public View view;

        public SeparatorHolder(View itemView) {
            super(itemView);
            ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(-1, UiUtils.dp2px(8));
            itemView.setLayoutParams(params);
            view = itemView;
        }
    }

    public class HeaderInfo {
        public String avatarUrl;
        public String username;
    }

    public class MenuInfo {
        public int position;
        public boolean showIcon;
        public String title;
        public String money;
        public String content;
        public String dotNumber;
        public boolean isSeparator;

        public MenuInfo(int position, boolean showIcon, String title, String money, String content, String
                dotNumber) {
            this.position = position;
            this.showIcon = showIcon;
            this.title = title;
            this.money = money;
            this.content = content;
            this.dotNumber = dotNumber;
        }
    }
}
